﻿@using System.Collections
@using Telerik.Web.Mvc.UI
@using Helena.Areas.Entradas.Models
@using Helena.Areas.Configuraciones.Models
@using Helena.Areas.Productos.Models
@using Helena.Areas.Proveedores.Models

@model IEnumerable<DetalleEntradaView>  

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
        
@{
    ViewBag.Title = "Detalle Entrada";
    Layout = "~/Views/Shared/_Layout.cshtml";
    EntradaView entrada = (EntradaView)ViewData["EntradaSeleccionada"];
}

@Html.Telerik().ScriptRegistrar().Globalization(true)

<script type="text/javascript">
    var Nombre;
    function onRowSelected(e) {
        var DetalleEntradaGrid = $('#DetalleEntrada').data('tGrid');
        Nombre = e.row.cells[0].innerHTML;
        $('#ProveedorNombre').text(Nombre);
        DetalleEntradaGrid.rebind();
    }

    function onDataBinding(e) {
        e.data = $.extend(e.data, { ProveedorNombre: Nombre });
    }

    function OnSubmitChanges(e) {
        var detallesGrid = $('#Detalles').data('tGrid');
        detallesGrid.rebind();
    }
    
    function onDataBindingDetalle(e) {
        e.data = $.extend(e.data, { EntradaID: @(Session["EntradaSeleccionada"].ToString())});
    }
</script> 

<div class="block big">
    <!-- Block Begin -->
    <!--Datos del comproducto compuesto -->
    <table style="width:100%">
        <tr>
            <th>Registro Interno de la Entrada</th>
            <td>@entrada.ID</td>
            <th>Numero de Guia</th>
            <td>@entrada.NumeroGuia</td>
            <th>Numero de Factura</th>
            <td>@entrada.NumeroFactura</td>
        </tr>
    </table>

<table style="width:100%">
    <tr>
        <td>
            <h3>Proveedores</h3>
            @(Html.Telerik().Grid((IEnumerable<ProveedorView>)ViewData["Proveedores"])
            .Name("Proveedores")
            .Columns(columns =>
            {
                columns.Bound(c => c.RazonSocial).Width(20);
            })
            .DataBinding(dataBinding => dataBinding.Ajax().Select("ObtenerProveedor", "DetalleEntrada"))
            .Filterable()
            .Pageable()
            .Sortable()
            .Selectable()
            .Scrollable(scrolling => scrolling.Height(200))
            .ClientEvents(events => events.OnRowSelect("onRowSelected"))
            
            .RowAction(row => 
            {
                row.Selected = row.DataItem.RazonSocial.Equals(ViewData["ProveedorNombre"]);
            })

            )
        </td>
        <td>
            <h3>Productos </h3>
            @(Html.Telerik().Grid((IEnumerable<DetalleEntradaView>)ViewData["DetalleEntrada"])
            .Name("DetalleEntrada")
            .Localizable("es-ES")
            .DataKeys(keys => keys.Add(c => c.ID))
            .Columns(columns=>
            {
                columns.ForeignKey(c => c.ProductoID, (IEnumerable)ViewData["Productos"], "ID", "Descripcion").Width(300);
                columns.Bound(c => c.Cantidad);
                columns.Bound(c => c.PrecioU).Format("{0:c}");
                columns.Bound(c => c.Igv).Format("{0:c}");
                columns.Bound(c => c.Subtotal).Format("{0:c}");
                columns.Bound(c => c.Total).Format("{0:c}");
                columns.Command(commands =>
                {
                    commands.Edit().ButtonType(GridButtonType.BareImage);
                }).Title("Agregar");
            })
            .DataBinding(dataBinding => dataBinding
                .Ajax()
                .Select("ObtenerProductos", "DetalleEntrada", new { ProveedorNombre = "HELENA TIENDAS S.A.C" })
                .Update("InsertarDetalleEntrada", "DetalleEntrada"))
                
            .ClientEvents(clientEvents => clientEvents.OnDataBinding("onDataBinding"))
            .ClientEvents(events => events.OnComplete("OnSubmitChanges"))            
            .Scrollable(scrolling => scrolling.Height(200))
            .Pageable()
            .Filterable()
            .Sortable()
            .Selectable()
            )
            </td>
    </tr>
</table>

    <table style="width:100%">
    <tr>
        <td>
            <h3>Detalles </h3>
            @(Html.Telerik().Grid(Model)
            .Name("Detalles")
            .Localizable("es-ES")
            .DataKeys(keys => keys.Add(x => x.ID))
                            .Editable(editing => editing
                            .Mode(GridEditMode.InLine)
                    ) 
            .Columns(columns=>
            {
                columns.Bound(x => x.NombreProducto).Width(300);
                columns.Bound(x => x.Cantidad);
                columns.Bound(x => x.PrecioU).Format("{0:c}");
                columns.Bound(x => x.Igv).Format("{0:c}");
                columns.Bound(x => x.Subtotal).Format("{0:c}");
                columns.Bound(x => x.Total).Format("{0:c}");
                columns.Command(commands =>
                {
                    commands.Delete().ButtonType(GridButtonType.BareImage);
                }).Width(80).Title("Modificar");
            })
            .DataBinding(dataBinding => dataBinding
                .Ajax()
                .Select("ObtenerDetalles", "DetalleEntrada", new { EntradaID = Session["EntradaSeleccionada"].ToString() })
                .Delete("EliminarDetalle", "DetalleEntrada"))

            .ClientEvents(clientEvents => clientEvents.OnDataBinding("onDataBindingDetalle"))
            .Scrollable(scrolling => scrolling.Height(200))
            .Pageable()
            .Filterable()
            .Sortable()                    
            )
            </td>
    </tr>
</table>


</div>
